package com.ymttest.business.service;

import com.ymatou.iapi.csim.parameter.*;
import com.ymatou.iapi.csim.service.GetMessageListCall;
import com.ymatou.iapi.csim.service.LoginCall;
import com.ymatou.iapi.csim.service.OnlineCall;
import com.ymatou.iapi.csim.service.OpenSessionCall;
import com.ymatou.iapi.csim.service.SendMessageCSCall;
import com.ymatou.iapi.csim.service.SendMessageCall;
import com.ymatou.iapi.csim.service.SendScoreMessageCall;
import com.ymt.core.tool.Logger;
import com.ymttest.common.enums.CallTypeEnum;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.model.ymt_onlinecs;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;

import org.apache.http.client.ClientProtocolException;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

public class CsimCallService {

	private int csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private Integer customerId=Integer.parseInt(EnvSetup.getData("customerId").toString()); 
	private Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	/**
	 * 用户发送消息：买家和买手 给码头洋洋 客服发送消息
	 * UserId：这里的用户Id是是指洋码头的买家和买手
	 * @return
	 */
	public SendMessageBean setSendMessageBean()
	{
		Logger.comment("设置用户发送消息SendMessageBean");
		SendMessageBean bean=new SendMessageBean();
		bean.setUserId(Integer.parseInt(TestDataManager.getRandomNum(5)));
		bean.setMessage("用户 给客服发消息自动化测试_"+TestDataManager.getRandomIntNum(5));
		bean.setMessageSource(1);
		bean.setSourceParams("App");
		bean.setCallType(CallTypeEnum.ordinaryCS.getValue());//咨询客服类型： 0 表示普通的客服   1表示AM客服
		bean.setQuestionGuideId(1);
		bean.setAppId("imyy.ymatou.com");
		return bean;
	}
	public SendMessageCall setSendMessageCall(SendMessageBean bean) throws ClientProtocolException, IOException
	{
		SendMessageCall call=new SendMessageCall();
		call.setData(bean);
		call.callService();
		return call;
	}

	/**
	 * 客服发送消息  客服发送消息主动给买家和买手
	 * CsId：客服id
	 * UserId：这里的用户Id是是指洋码头的买家和买手
	 * @return
	 */
	public SendMessageCSBean setSendMessageCSBean()
	{
		Logger.comment("设置用户发送消息SendMessageBean");
		SendMessageCSBean bean=new SendMessageCSBean();
		bean.setCsId(Integer.parseInt(TestDataManager.getRandomNum(5)));
		bean.setUserId(Integer.parseInt(TestDataManager.getRandomNum(5)));
		bean.setMessage("客服发送消息 自动化测试_"+TestDataManager.getRandomIntNum(5));
		bean.setSessionId("1");
		bean.setAppId("imyy.ymatou.com");
		return bean;
	}
	public SendMessageCSCall setSendMessageCSCall(SendMessageCSBean bean) throws ClientProtocolException, IOException
	{
		SendMessageCSCall call=new SendMessageCSCall();
		call.setData(bean);
		call.callService();
		return call;
	}
	/**
	 * 获取消息列表
	 * @return
	 */
	public  GetMessageListBean GetMessageListBean()
	{
		Logger.comment("设置获取消息列表SendMessageBean");
		GetMessageListBean bean=new GetMessageListBean();
		Date dNow = new Date(); // 当前时间
		Date date = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		date = calendar1.getTime(); // 得到前一天的时间

		bean.setFromuserId(customerId);
		bean.setToUserId(userId);
		bean.setIsNew(true);
		bean.setTimeline(date);
		bean.setLongTimeline(date.getTime());
		bean.setMessageCount(1000);
		bean.setSessionNumber("100");
		bean.setCallType(CallTypeEnum.ordinaryCS.getValue());
		return bean;
	}
	public GetMessageListCall GetMessageListCall(GetMessageListBean bean) throws ClientProtocolException, IOException
	{
		GetMessageListCall call=new GetMessageListCall();
		call.setData(bean);
		call.callService();
		return call;
	}
	/**
	 * 客服主动开启新会话
	 * @return
	 */
	public OpenSessionBean setOpenSessionBean()
	{
		Logger.comment("设置客服主动开启新会话OpenSessionBean");
		OpenSessionBean bean=new OpenSessionBean();
		bean.setCsId(csId);
		bean.setToUserId(userId);
		bean.setToUserId(Integer.parseInt(TestDataManager.getRandomNum(5)));
		bean.setMessage("客服主动开启新会话_自动化测试_"+TestDataManager.getRandomIntNum(5));
		return bean;
	}
	public OpenSessionCall OpenSessionCall(OpenSessionBean bean) throws ClientProtocolException, IOException
	{
		OpenSessionCall call=new OpenSessionCall();
		call.setData(bean);
		call.callService();
		return call;
	}
	
	/**
	 * 客服上线，可以接收新会话
	 * @return
	 */
	public OnlineBean setOnlineBean()
	{
		Logger.comment("设置客服登录上线Online");
		OnlineBean bean=new OnlineBean();
		bean.setCsId(csId);
		return bean;
	}
	public OnlineCall OnlineCall(OnlineBean bean) throws ClientProtocolException, IOException
	{
		OnlineCall call=new OnlineCall();
		call.setData(bean);
		call.callService();
		return call;
	}

    
	
	/**
	 * 客服登录
	 * @return
	 */
	public LoginBean setLoginBean()
	{
		Logger.comment("设置客服登录上线Login");
		LoginBean bean=new LoginBean();
		bean.setCsId(csId);
		bean.setCsName("买家客服1");
		bean.setCsNickName("买家昵称1");
		bean.setCsType(0);
		bean.setSocketIoToken("1111");
		bean.setAreaName("CN");
		return bean;
	}
	public LoginCall LoginCall(LoginBean bean) throws ClientProtocolException, IOException
	{
		LoginCall call=new LoginCall();
		call.setData(bean);
		call.callService();
		return call;
	}
	/**
	 * 客服下线，不接收新会话
	 * @return
	 */
	public OfflineBean setOfflineBean()
	{
		Logger.comment("设置客服下线Offline");
		OfflineBean bean=new OfflineBean();
		bean.setOffLineType(1);//下线类型(1：正常退出 2：浏览器关闭 3：客户端超时)
		bean.setSocketIoToken("111");//	socket标识
		return bean;
	}
	public com.ymatou.iapi.csim.service.OfflineCall OfflineCall(OfflineBean bean) throws ClientProtocolException, IOException
	{
		com.ymatou.iapi.csim.service.OfflineCall call=new  com.ymatou.iapi.csim.service.OfflineCall();
		call.setData(bean);
		call.callService();
		return call;
	}
	/**
	 * A客服主动转接给B客服
	 * @return
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public RevolveSessionBean setRevolveSession() throws ClientProtocolException, IOException
	{
		Logger.comment("设置客服转接RevolveSession");
		RevolveSessionBean bean=new RevolveSessionBean();
		bean.setFromCSId(Integer.parseInt(TestDataManager.getRandomNum(5)));
		bean.setToCSId(Integer.parseInt(TestDataManager.getRandomNum(5)));
		return bean;
	}
	public com.ymatou.iapi.csim.service.RevolveSessionCall RevolveSessionCall(RevolveSessionBean bean) throws ClientProtocolException, IOException
	{
		com.ymatou.iapi.csim.service.RevolveSessionCall call=new com.ymatou.iapi.csim.service.RevolveSessionCall();
		call.callService();
		return call;
	}
    public com.ymatou.iapi.csim.service.AssignSellerUserCall getAssignSellerUserCall() throws ClientProtocolException, IOException
    {
    	com.ymatou.iapi.csim.service.AssignSellerUserCall call=new com.ymatou.iapi.csim.service.AssignSellerUserCall();
    	call.callService();
    	return call;
    }
    
    public com.ymatou.iapi.csim.service.AssignBuyerUserCall getAssignBuyerUserCall() throws ClientProtocolException, IOException
    {
    	com.ymatou.iapi.csim.service.AssignBuyerUserCall call=new com.ymatou.iapi.csim.service.AssignBuyerUserCall();
    	call.callService();
    	return call;
    }
	
	public  ymt_onlinecs getOnlineCS() throws InterruptedException, ClientProtocolException, IOException
	{
		ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
   	   //1:查询没有客服，就登录，创建客服
		int conline_count=onlinecsWapper.selectBy_Count(MapUtil.hashMap("CSId",csId,"Status",0));
		if(conline_count==0){
    		//客服登录
    		LoginBean loginBean=setLoginBean();
    		LoginCall(loginBean);
		}
		//2:查询客服不在线就调用客服上线接口
		ymt_onlinecs onlinecs2=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csId)).get(0);
		if(onlinecs2!=null &&onlinecs2.getStatus()!=0)
		{
    		//客服上线
    		OnlineBean onlinebean=setOnlineBean();
    		OnlineCall(onlinebean);
		}
		return onlinecs2;
	}
	
	/**
	 * 买家客服上线
	 * @param csId
	 * @return
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public  ymt_onlinecs getOnlineCS(int csId) throws InterruptedException, ClientProtocolException, IOException
	{
		ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
   	   //1:查询没有客服，就登录，创建客服
		int conline_count=onlinecsWapper.selectBy_Count(MapUtil.hashMap("CSId",csId,"Status",0));
		if(conline_count==0){
    		//客服登录
    		LoginBean loginBean=setLoginBean();
    		loginBean.setCsId(csId);
    		LoginCall(loginBean);
		}
		//2:查询客服不在线就调用客服上线接口
		ymt_onlinecs onlinecs2=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csId)).get(0);
		if(onlinecs2!=null &&onlinecs2.getStatus()!=0)
		{
    		//客服上线
    		OnlineBean onlinebean=setOnlineBean();
    		onlinebean.setCsId(csId);
    		OnlineCall(onlinebean);
		}
		return onlinecs2;
	}
	
	
	
	/**
	 * 买手客服上线
	 * @param csId
	 * @return
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public ymt_onlinecs getOnlineCS_Seller(int... csId_sellerId) throws InterruptedException, ClientProtocolException, IOException
	{
		int csId=0;
		if(csId_sellerId==null||csId_sellerId.length==0)
			csId=TestDataManager.getRandomIntNum(5);
		else
			csId=csId_sellerId[0];
		ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
   	   //1:查询没有客服，就登录，创建客服
		int conline_count=onlinecsWapper.selectBy_Count(MapUtil.hashMap("CSId",csId,"Status",0));
		if(conline_count==0){
    		//客服登录
    		LoginBean loginBean=setLoginBean();
    		loginBean.setCsType(1);
    		loginBean.setCsId(csId);
    		LoginCall(loginBean);
		}
		//2:查询客服不在线就调用客服上线接口
		ymt_onlinecs onlinecs2=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csId)).get(0);
		if(onlinecs2!=null &&onlinecs2.getStatus()!=0)
		{
    		//客服上线
    		OnlineBean onlinebean=setOnlineBean();
    		onlinebean.setCsId(csId);
    		OnlineCall(onlinebean);
		}
		return onlinecs2;
	}
	public ymt_cssession getOpenSession(int csId,int userId) throws InterruptedException, ClientProtocolException, IOException
	{
		ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
		//3:查询是否有会话
		int count=csSessionWapper.selectBy_Count(MapUtil.hashMap("UserId",userId,"Status",0));
		OpenSessionCall openSessionCall=new OpenSessionCall();
		ymt_cssession csSession=new ymt_cssession();
		if(count==0){
			//主动开启会话
			OpenSessionBean openSessionBean=setOpenSessionBean();
			openSessionBean.setCsId(csId);
			openSessionBean.setToUserId(userId);
			openSessionCall=OpenSessionCall(openSessionBean);
			//csSession.setId(openSessionCall.getSeesionId());
		}
		//else{
		    csSession=csSessionWapper.selectBy(MapUtil.hashMap("UserId",userId,"Status",0));
		//}
		return csSession;
	}
	/**
	 * 客服发送评分
	 * @param csId
	 * @param userId
	 * @return
	 * @throws ClientProtocolException
	 * @throws InterruptedException
	 * @throws IOException
	 */
	public SendScoreMessageBean setSendScoreMessageBean(int csId,int userId) throws ClientProtocolException, InterruptedException, IOException
	{
		SendScoreMessageBean bean=new SendScoreMessageBean();
		//查询是否有在线客服 1，2
		 getOnlineCS();
		//打开会话
		ymt_cssession csSession=getOpenSession(csId,userId);
		bean.setCsId(csId);
		bean.setUserId(userId);
		bean.setSessionId(csSession.getId());
		return bean;
	}
	public SendScoreMessageCall SendScoreMessageCall(SendScoreMessageBean bean) throws ClientProtocolException, IOException
	{
		SendScoreMessageCall call=new SendScoreMessageCall();
		call.setData(bean);
		call.callService();
		return call;
	}
}

